package com.zyc.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.zyc.commonUtils.R;
import com.zyc.eduservice.client.VodClient;
import com.zyc.eduservice.entity.EduVideo;
import com.zyc.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-19
 */
@Api("小节管理")
@RestController
@RequestMapping("/eduservice/edu-video")
@CrossOrigin //解决跨域
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    @ApiOperation(value = "新增小节")
    @PostMapping("/addVideo")
    public R addVideo(
            @ApiParam(name = "video", value = "小节对象", required = true)
            @RequestBody EduVideo video) {

        eduVideoService.save(video);
        return R.ok();
    }

    @ApiOperation(value = "根据ID修改小节")
    @PutMapping("updateVideo")
    public R update(
            @ApiParam(name = "video", value = "小节对象", required = true)
            @RequestBody EduVideo video) {

        eduVideoService.updateById(video);
        return R.ok();
    }

    @ApiOperation("根据id逻辑删除小节")
    @DeleteMapping("deleteVideo/{id}")
    public R deleteVideo(
            @ApiParam(name = "id", value = "小节ID", required = true)
            @PathVariable String id) {

        EduVideo video = eduVideoService.getById(id);
        System.out.println(video);
        String videoSourceId = video.getVideoSourceId();
        if (videoSourceId != null && !StringUtils.isEmpty(videoSourceId)) {
            vodClient.deleteVideo(videoSourceId);
        }

        eduVideoService.removeById(id);
        return R.ok();
    }

    @ApiOperation("根据id查询小节")
    @GetMapping("getVideo/{id}")
    public R getVideo(
            @ApiParam(name = "id", value = "小节ID", required = true)
            @PathVariable String id) {

        EduVideo video = eduVideoService.getById(id);
        return R.ok().data("video",video);
    }
}

